package client

import (
	"gorm.io/gorm"
	"likeadmin/admin/schemas/req"
	"likeadmin/admin/schemas/resp"
	"likeadmin/core/request"
	"likeadmin/core/response"
	"likeadmin/model/client"
	"likeadmin/util"
)

type IDramaSeriesService interface {
	List(page request.PageReq, listReq req.DramaSeriesListReq) (res response.PageResp, e error)
	Detail(id string) (res resp.DramaSeriesResp, e error)
	Add(addReq req.DramaSeriesAddReq) (e error)
	Edit(editReq req.DramaSeriesEditReq) (e error)
	Del(id string) (e error)
}

// NewDramaSeriesService 初始化
func NewDramaSeriesService(db *gorm.DB) IDramaSeriesService {
	return &dramaSeriesService{db: db}
}

// dramaSeriesService 短剧剧集服务实现类
type dramaSeriesService struct {
	db *gorm.DB
}

// List 短剧剧集列表
func (srv dramaSeriesService) List(page request.PageReq, listReq req.DramaSeriesListReq) (res response.PageResp, e error) {
	// 分页信息
	limit := page.PageSize
	offset := page.PageSize * (page.PageNo - 1)
	// 查询
	model := srv.db.Model(&client.DramaSeries{})
	if listReq.DramaId != "" {
		model = model.Where("drama_id = ?", listReq.DramaId)
	}
	if listReq.Title != "" {
		model = model.Where("title like ?", "%"+listReq.Title+"%")
	}
	if listReq.IsVip >= 0 {
		model = model.Where("is_vip = ?", listReq.IsVip)
	}
	// 总数
	var count int64
	err := model.Count(&count).Error
	if e = response.CheckErr(err, "List Count err"); e != nil {
		return
	}
	// 数据
	var objs []client.DramaSeries
	err = model.Limit(limit).Offset(offset).Order("create_time asc").Find(&objs).Error
	if e = response.CheckErr(err, "List Find err"); e != nil {
		return
	}
	resps := []resp.DramaSeriesResp{}
	response.Copy(&resps, objs)

	for i := range resps {
		resps[i].VideoUrl = ""
	}

	return response.PageResp{
		PageNo:   page.PageNo,
		PageSize: page.PageSize,
		Count:    count,
		Lists:    resps,
	}, nil
}

// Detail 短剧剧集详情
func (srv dramaSeriesService) Detail(id string) (res resp.DramaSeriesResp, e error) {
	var obj client.DramaSeries
	err := srv.db.Where("series_id = ?", id).Limit(1).First(&obj).Error
	if e = response.CheckErrDBNotRecord(err, "数据不存在!"); e != nil {
		return
	}
	if e = response.CheckErr(err, "Detail First err"); e != nil {
		return
	}
	response.Copy(&res, obj)

	res.VideoUrl = util.UrlUtil.ToAbsoluteUrl(res.VideoUrl)

	return
}

// Add 短剧剧集新增
func (srv dramaSeriesService) Add(addReq req.DramaSeriesAddReq) (e error) {
	var obj client.DramaSeries

	err := srv.db.Where("drama_id = ?", addReq.DramaId).Limit(1).Order("sort desc").First(&obj).Error
	if e = response.CheckErrDBNotRecord(err, "短剧数据不存在!"); e != nil {
		obj.Sort = 1
	} else {
		obj.Sort = obj.Sort + 1
	}

	response.Copy(&obj, addReq)
	obj.VideoUrl = util.UrlUtil.ToRelativeUrl(obj.VideoUrl)

	obj.SeriesId = util.UitlSnow.SnowId()
	err = srv.db.Create(&obj).Error
	e = response.CheckErr(err, "Add Create err")
	return
}

// Edit 短剧剧集编辑
func (srv dramaSeriesService) Edit(editReq req.DramaSeriesEditReq) (e error) {
	var obj client.DramaSeries
	err := srv.db.Where("series_id = ?", editReq.SeriesId).Limit(1).First(&obj).Error
	// 校验
	if e = response.CheckErrDBNotRecord(err, "数据不存在!"); e != nil {
		return
	}
	if e = response.CheckErr(err, "Edit First err"); e != nil {
		return
	}
	// 更新
	response.Copy(&obj, editReq)

	obj.VideoUrl = util.UrlUtil.ToRelativeUrl(obj.VideoUrl)

	err = srv.db.Model(&client.DramaSeries{}).Where("series_id = ?", editReq.SeriesId).Select("series_id", "title", "video_url", "is_vip", "sort").Updates(client.DramaSeries{
		SeriesId: editReq.SeriesId,
		Title:    editReq.Title,
		VideoUrl: editReq.VideoUrl,
		IsVip:    editReq.IsVip,
		Sort:     editReq.Sort,
	}).Error
	e = response.CheckErr(err, "Edit Updates err")
	return
}

// Del 短剧剧集删除
func (srv dramaSeriesService) Del(id string) (e error) {
	var obj client.DramaSeries
	err := srv.db.Where("series_id = ?", id).Limit(1).First(&obj).Error
	// 校验
	if e = response.CheckErrDBNotRecord(err, "数据不存在!"); e != nil {
		return
	}
	if e = response.CheckErr(err, "Del First err"); e != nil {
		return
	}
	// 删除
	err = srv.db.Delete(&obj).Error
	e = response.CheckErr(err, "Del Delete err")
	return
}
